<template>
  <a-layout style="min-height: 100vh">
    <a-layout-sider v-model:collapsed="collapsed" collapsible>
      <div class="logo">博客后台</div>
      <a-menu
        v-model:selectedKeys="selectedKeys"
        theme="dark"
        mode="inline"
      >
        <a-menu-item key="dashboard">
          <dashboard-outlined />
          <span>仪表盘</span>
          <router-link to="/" />
        </a-menu-item>
        <a-menu-item key="article">
          <file-outlined />
          <span>文章管理</span>
          <router-link to="/article/list" />
        </a-menu-item>
        <a-menu-item key="category">
          <appstore-outlined />
          <span>分类管理</span>
          <router-link to="/category/list" />
        </a-menu-item>
        <a-menu-item key="comment">
          <comment-outlined />
          <span>评论管理</span>
          <router-link to="/comment/list" />
        </a-menu-item>
      </a-menu>
    </a-layout-sider>
    <a-layout>
      <a-layout-header style="background: #fff; padding: 0">
        <div class="header-right">
          <a-dropdown>
            <a class="ant-dropdown-link" @click.prevent>
              {{ userStore.userInfo.nickname || userStore.userInfo.username }} <down-outlined />
            </a>
            <template #overlay>
              <a-menu>
                <a-menu-item key="0">
                  <a @click="handleLogout">退出登录</a>
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
      </a-layout-header>
      <a-layout-content style="margin: 0 16px">
        <router-view></router-view>
      </a-layout-content>
      <a-layout-footer style="text-align: center">
        博客管理系统 ©2023
      </a-layout-footer>
    </a-layout>
  </a-layout>
</template>

<script lang="ts" setup>
import { ref, computed, watch, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { DashboardOutlined, FileOutlined, AppstoreOutlined, DownOutlined, CommentOutlined } from '@ant-design/icons-vue';
import { message } from 'ant-design-vue';
import { useUserStore } from '@/stores/user';
import { useCategoryStore } from '@/stores/category';

const route = useRoute();
const router = useRouter();
const userStore = useUserStore();
const categoryStore = useCategoryStore();
const collapsed = ref(false);

// 根据路由设置选中的菜单项
const selectedKeys = computed(() => {
  const path = route.path;
  if (path === '/') return ['dashboard'];
  if (path.includes('/article')) return ['article'];
  if (path.includes('/category')) return ['category'];
  if (path.includes('/comment')) return ['comment'];
  return [];
});

const handleLogout = async () => {
  try {
    await userStore.logout();
    message.success('退出登录成功');
    router.push('/login');
  } catch (error) {
    console.error('退出登录失败:', error);
  }
};

onMounted(async () => {
  await categoryStore.getCategoryList(); // 获取分类数据
});
</script>

<style scoped>
.logo {
  height: 32px;
  margin: 16px;
  color: white;
  font-size: 18px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.header-right {
  display: flex;
  justify-content: flex-end;
  padding-right: 20px;
}
</style> 